from typing import *
from leetcode.editor.common.node import *


# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def maxSum(self, nums: List[int], m: int, k: int) -> int:
        window = {} # 记录定长的窗口中有几种数字
        ans = 0
        win_sum = 0
        for right,item in enumerate(nums):
            window[item] = window.get(item,0)+1
            win_sum+=item
            left = right-k+1
            if left >=0:
                if len(window) >=m:
                    ans = max(ans,win_sum)
                l = nums[left]
                window[l]-=1
                if window[l]==0:
                    del window[l]
                win_sum-=l
                left +=1
        return ans
# leetcode submit region end(Prohibit modification and deletion)


if __name__ == '__main__':
    solution = Solution()
    print(solution.maxSum([1,2,2],2,2))
    # your test code here